Ext.define('Emp.controller.ViewportController', {
    extend: 'Ext.app.Controller',
    
    models: ['Resource'],
    views: [
        'Viewport',
        'Wester',
        'Viewer'
    ],
    refs: [
    	{ref: 'viewer', selector: 'xviewer'},
    	{ref: 'wester', selector: 'xwester'}
    ],

    init: function() {
        this.control({
            'viewport xwester': {
                'itemclick': this.doItemClick
            }
        });
    },
    
    doItemClick: function(dataView, record, item, index, e, options) {
		var me = this;
		var tabpanel = me.getViewer();
		var target = record.get('target');
		if(Ext.String.trim(target) !== '') {
			var controller = me.application.controllers.get(target + 'Controller');
			if(!controller) {
				controller = me.application.getController(target + 'Controller');
				controller.init();
			}
			target = target.toLowerCase() + 'list';
			var tab = tabpanel.down(target);
			if (tab == null && !Ext.isObject(tab)) {
				tab = Ext.widget(target, {
					title: record.get('text'),
					iconCls: record.get('iconCls'),
					closable: true
				});
				tabpanel.add(tab).show();
			} else {
				tab.show();
			}
		}
	}
});